<?php  if ( ! defined('DIR_ROOT')) exit(NO_SCRIPT_ALLOW_NOTICE);
class CK_VarCache 
{
    private $cache_store = array();
    function CK_VarCache()
    {
        //TODO 加载缓存文件
        
        $file = @file_get_contents(DIR_CACHE."/se.php");
        if($file == null)
        {
            $this->cache_store = null ;
        }
        else
        {
          $this->cache_store = unserialize($file);  
        }
    }
    
    /**
     * 
     * @param string $key
     * @param string $val
     * @param int $exp
     */
    function add ($key, $val, $exp = 0)
    {
        /*有则返回false，没有则添加*/
       if(isset($this->cache_store[$key]))
       {
//           $this->cache_store[$key]['v'] = $val;
//           $exp == 0 ? $this->cache_store[$key]['t'] = time() + $exp : $this->cache_store[$key]['t'] = 0 ;
           return false;           
       }
       else
       {
           $this->cache_store[$key]['v']=$val;
           $exp == 0 ? $this->cache_store[$key]['t'] = time() + $exp : $this->cache_store[$key]['t'] = 0 ;
           $this->cache_store[$key]['e']  = $exp;
           return true;
       }
    }
    
    function delete ($key, $time = 0)
    {
        unset($this->cache_store[$key]);
        return true;
    }
    
    function get ($key)
    {
        if(isset($this->cache_store[$key]))
        {
            if( $this->cache_store[$key]['t'] < time())
            {
                return $this->cache_store[$key]['v'];
            }
            else
            {
                unset($this->cache_store[$key]);
            }
        }
        else
        {
            return NULL;
        }
    }
    
    function get_multi ($keys)
    {
        
    }
    /**
     * 
     * @param $key
     * @param $amt
     */
    function incr ($key, $amt=1)
    {
        if(isset($this->cache_store[$key]) && $this->cache_store[$key]['t'] < time())
        {
            if(is_numeric($this->cache_store[$key]['v']))
            {
                $this->cache_store[$key]['v'] = $this->cache_store[$key]['v'] + $amt;
                $this->cache_store[$key]['t'] = $this->cache_store[$key]['t'] + $this->cache_store[$key]['e'] ;
            }
            return $this->cache_store[$key]['v'] ;
        }
        else
        {
            return NULL;
        }
    }

    function replace ($key, $value, $exp=0)
    {
       $this->cache_store[$key]['v']=$value;
       $exp == 0 ? $this->cache_store[$key]['t'] = time() + $exp : $this->cache_store[$key]['t'] = 0 ;
       $this->cache_store[$key]['e']  = $exp;
       return true;
    }

    function set ($key, $value, $exp=0)
    {
       $this->cache_store[$key]['v']=$value;
       $exp == 0 ? $this->cache_store[$key]['t'] = time() + $exp : $this->cache_store[$key]['t'] = 0 ;
       $this->cache_store[$key]['e']  = $exp;
       return true;
    }
    
    function __destruct()
    {
        //TODO 写入缓存文件
        file_put_contents(DIR_CACHE."/se.php",serialize($this->cache_store) ); 
        
    }
}